// pages/works/works.js
const app = getApp()
Page({
    data: {
        activeNames: ['0'],
        id: 0,
        num_sn: '',
        ta_years_desc: '',
        ta_month_desc: '',
        ta_day_desc: '',
        me_years_desc: '',
        me_month_desc: '',
        me_day_desc: '',
        ta_wants: '',
        me_tuijin: '',
        me_tuijin_degree: '',
        ta_xintai: '',
        me_xintai: '',
        ta_xinli: '',
        me_xinli: '',
        ta_hope_me: '',
        // 用户提问信息
        person_tpl: '',
        // 用户来源的 history_id,
        history_id: 0,
        queryTypes: 0,
        itypes: 0,
        theme:'',       
    },
    onShow: function () {
        if(wx.getStorageSync('theme') == 'style1'){
          this.setData({
            theme:'style1',
    
          })
      }else{
          this.setData({
            theme:'init',
          })
      }},
    /**
     * 折叠面板
     */
    changeIcon(e) {
        let seq = e.currentTarget.dataset.seq;
        let arr = this.data.activeNames;

        if (arr.indexOf(seq) > 0) {
            arr[seq] = 0;
            this.data.activeNames = arr;
            this.setData({
                activeNames: arr
            });
            // console.log('有--删除::', arr, this.data.activeNames);
        } else {
            arr[seq] = seq;
            this.data.activeNames = arr;
            this.setData({
                activeNames: arr
            });
            // console.log('无--删除::', arr, this.data.activeNames);        
        }
    },    
    /**
     * 折叠面板
     */
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取页面参数
        this.data.id = (options.id == 'undefined') ? 0 : options.id;    
        // 如果是从个人中心点击过来
        if (options.history_id) {
            this.setData({
                history_id: options.history_id
            });
        }    
        // 大菜单类型
        this.data.queryTypes = wx.getStorageSync('type') ? wx.getStorageSync('type') : 0;
        // 如果从 个人中心,历史记录 > 结果页面
        if (options.types) {
            this.data.queryTypes = options.types;
            this.setData({
                queryTypes: options.types
            });            
        }

        var reqUri = app.globalData.domain + 'v3/index/v3latestPersonPredict';
        // 大菜单入口下的, 咨询类目
        var itypes = wx.getStorageSync('itype') ? wx.getStorageSync('itype') : 0;
        // 如果是 个人中心跳转, 会带上 itypes
        if (options.itypes)  {
            itypes = options.itypes;
            this.setData({
                itypes: options.itypes
            });              
        }
                
        var msg = wx.getStorageSync('message') ? wx.getStorageSync('message') : '';
        var personJob = wx.getStorageSync('person_job') ? wx.getStorageSync('person_job') : '';

        if (this.data.id) reqUri = app.globalData.domain + 'v3/index/v3latestPersonPredict?id=' + this.data.id + '&history_id='+this.data.history_id;
        var that = this;
        // 获取用户信息缓存
        var cacheUserInfo = wx.getStorageSync('user_phone');
        var comboType = wx.getStorageSync('combo_type');

        if (!cacheUserInfo.phoneNumber  && options.is_share != 'undefined') {
            wx.showModal({
                title: '温馨提示',
                content: '请先进行授权',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/login/index',
                        })
                    }
                }
            })

            return;
        }

        
        reqUri = app.globalData.domain + 'v4/index/getPredict?history_id=' + this.data.history_id;
        wx.request({
            url: reqUri,
            data: {
                comboType: comboType,
                types: this.data.queryTypes,
                itypes: itypes,
                person_fix_msg: msg,
                person_job: personJob,
                user_phone: (cacheUserInfo.phoneNumber && typeof (cacheUserInfo.phoneNumber) != 'undefined') ? cacheUserInfo.phoneNumber : '18888888888'
            },
            success(res) {
                if (res.data.code == app.globalData.successCode) {
                    that.setData({
                        id: res.data.data.id,
                        num_sn: res.data.data.num_sn,
                        ta_years_desc: res.data.data.ta_years_desc,
                        ta_month_desc: res.data.data.ta_month_desc,
                        ta_day_desc: res.data.data.ta_day_desc,
                        me_years_desc: res.data.data.me_years_desc,
                        me_month_desc: res.data.data.me_month_desc,
                        me_day_desc: res.data.data.me_day_desc,
                        ta_wants: res.data.data.ta_wants,
                        me_tuijin: res.data.data.me_tuijin,
                        me_tuijin_degree: res.data.data.me_tuijin_degree,
                        ta_xintai: res.data.data.ta_xintai,
                        me_xintai: res.data.data.me_xintai,
                        ta_xinli: res.data.data.ta_xinli,
                        me_xinli: res.data.data.me_xinli,
                        ta_hope_me: res.data.data.ta_hope_me,
                        person_tpl: res.data.data.person_tpl,
                        history_id: res.data.data.history_id,
                        itypes: res.data.data.itypes
                    });
                } else {
                    // 非历史记录页面的数据, 弹出错误信息
                    if (!that.data.id) {
                        wx.showModal({
                            title: '错误提示',
                            content: res.data.msg,
                            showCancel: false,
                            success (res) {
                                if (res.confirm) {
                                    wx.navigateTo({
                                      url: '/pages/newMenu/index',
                                    })
                                }
                            }
                        });
                    }                    
                }
            }
        })
    },    
    // 发送给朋友
    onShareAppMessage: function () {
        var that = this;

        return {
            title: '天纪云',
            path: 'pages/emotion/emotion?id=' + that.data.id + '&is_share=1' + '&history_id=' + that.data.history_id + '&types=' + that.data.queryTypes + '&itypes=' + that.data.itypes,
            // imageUrl: '../../resources/logo.jpg'
        }
    },
    // 分享到朋友圈
    onShareTimeline: function () {
        var that = this;

        return {
            title: '天纪云',
            path: 'pages/emotion/emotion?id=' + that.data.id + '&is_share=1' + '&history_id=' + that.data.history_id + '&types=' + that.data.queryTypes + '&itypes=' + that.data.itypes,
            // imageUrl: '../../resources/logo.jpg'
        }
    }
})